package top.xmj.controller;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xmj.domain.User;
import top.xmj.service.UserService;
import top.xmj.util.JWTUtils;
import top.xmj.vo.ResultVO;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 *
 * @author 冰咖啡
 * @Date:2/12/2022 下午 9:36
 * @Description: 用户操作
 *
 */
@RestController
@RequestMapping("/user")
@ApiModel("用户操作接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultVO login(@RequestBody User user, HttpServletResponse response) {
        System.out.println("111111"+user);
        Map<String, Object> map = userService.login(user);
//        response.setContentType("text/html; charset=utf-8");
//        response.setCharacterEncoding("utf-8");
        //将token存入Http的header中
        response.setHeader(JWTUtils.USER_LOGIN_TOKEN, (String) map.get("token"));
        if(map.get("user")==null) {
            return new ResultVO(404,"登录失败，用户名或密码错误！",null);
        }
        return new ResultVO(200,"登陆成功",map);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public ResultVO logout(){
        return new ResultVO(200,"退出登录",null);
    }

    @GetMapping("/user")
    @ApiOperation("获取用户列表")
    public ResultVO getUsers(){
        List<User> users = userService.getUsers();
        System.out.println(users);
        return new ResultVO(200,"获取成功",users);
    }

}
